package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderInfoPo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.po.OrderInfoSubPo;
import com.ymatou.iapi.trading.operate.parameter.po.SubcatalogsPo;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeOrderDiscountBean;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.ChangeOrderDiscountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 修改订单折扣和运费(加价或减价) FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderDiscount {

	private static ChangeOrderDiscountBean changeorderdiscountbean;

	private static ChangeOrderDiscountCall changeorderdiscountcall;

	private static DefaultWapper database = new DefaultWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改订单折扣和运费");
	}

	@Before
	public void caseUp() {

		changeorderdiscountbean = new ChangeOrderDiscountBean();
		changeorderdiscountcall = new ChangeOrderDiscountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeorderdiscountcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 
	 * 一次下单同 一个商家 2个订单号,不使用优惠券,每个订单下面3个商品,商品包邮
	 * 
	 */
	private static PlaceOrderResp placeOrder6() {

		PlaceOrderReq placeorderBean = new PlaceOrderReq();

		int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

		placeorderBean = PlaceOrderService.getDefaultOrderReq();

		//不使用平台券
		placeorderBean.setYmatouCoupon(null);

		List<OrderGroupVo> orderGroupVoList = new ArrayList<OrderGroupVo>();

		OrderGroupVo orderGroupVo = PlaceOrderService.getDefaultOrderGroupVo(
				userId, sellerId);

		orderGroupVo.setSellerCoupon(null);

		orderGroupVo.getOrders().add(
				PlaceOrderService.getDefaultOrderVo(userId));

		orderGroupVoList.add(orderGroupVo);

		//每个订单项下面再添加2个商品，一共是3个商品
		for (int i = 0; i < 2; i++) {
			orderGroupVoList
					.get(0)
					.getOrders()
					.forEach(
							order -> order.getOrderItems().add(
									PlaceOrderService.getDefaultOrderItemVo()));
		}

		//设置订单商家组
		placeorderBean.setOrderGroups(orderGroupVoList);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	/**
	 * 
	 * 一次下单同 一个商家 2个订单号,不使用优惠券,每个订单下面3个商品,商品不包邮，随机运费
	 * 
	 */
	private static PlaceOrderResp placeOrder7() {

		PlaceOrderReq placeorderBean = new PlaceOrderReq();

		int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

		placeorderBean = PlaceOrderService.getDefaultOrderReq();

		//不使用平台券
		placeorderBean.setYmatouCoupon(null);

		List<OrderGroupVo> orderGroupVoList = new ArrayList<OrderGroupVo>();

		OrderGroupVo orderGroupVo = PlaceOrderService.getDefaultOrderGroupVo(
				userId, sellerId);

		orderGroupVo.setSellerCoupon(null);

		orderGroupVo.getOrders().add(
				PlaceOrderService.getDefaultOrderVo(userId));

		orderGroupVoList.add(orderGroupVo);

		//每个订单项下面再添加2个商品，一共是3个商品
		for (int i = 0; i < 2; i++) {
			orderGroupVoList
					.get(0)
					.getOrders()
					.forEach(
							order -> order.getOrderItems().add(
									PlaceOrderService.getDefaultOrderItemVo()));
		}

		//设置订单商家组
		placeorderBean.setOrderGroups(orderGroupVoList);

		//设置没有商品的随机运费
		for (int i = 0; i < 1; i++) {

			for (int j = 0; j < placeorderBean.getOrderGroups().get(i)
					.getOrders().size(); j++) {

				placeorderBean
						.getOrderGroups()
						.get(i)
						.getOrders()
						.get(j)
						.getOrderItems()
						.forEach(
								item -> item.setFreight(new BigDecimal(
										new Random().nextInt(50))));

			}

		}

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	/**
	 * 
	 * 一次下单同 一个商家 2个订单号,不使用优惠券,每个订单下面3个商品,商品不包邮，第二个商品运费为0
	 * 
	 */
	private static PlaceOrderResp placeOrder8() {

		PlaceOrderReq placeorderBean = new PlaceOrderReq();

		int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
		int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

		placeorderBean = PlaceOrderService.getDefaultOrderReq();

		//不使用平台券
		placeorderBean.setYmatouCoupon(null);

		List<OrderGroupVo> orderGroupVoList = new ArrayList<OrderGroupVo>();

		OrderGroupVo orderGroupVo = PlaceOrderService.getDefaultOrderGroupVo(
				userId, sellerId);

		orderGroupVo.setSellerCoupon(null);

		orderGroupVo.getOrders().add(
				PlaceOrderService.getDefaultOrderVo(userId));

		orderGroupVoList.add(orderGroupVo);

		//每个订单项下面再添加2个商品，一共是3个商品
		for (int i = 0; i < 2; i++) {
			orderGroupVoList
					.get(0)
					.getOrders()
					.forEach(
							order -> order.getOrderItems().add(
									PlaceOrderService.getDefaultOrderItemVo()));
		}

		//设置订单商家组
		placeorderBean.setOrderGroups(orderGroupVoList);

		//设置商品的随机运费
		for (int i = 0; i < 1; i++) {

			for (int j = 0; j < placeorderBean.getOrderGroups().get(i)
					.getOrders().size(); j++) {

				placeorderBean
						.getOrderGroups()
						.get(i)
						.getOrders()
						.get(j)
						.getOrderItems()
						.forEach(
								item -> item.setFreight(new BigDecimal(
										new Random().nextInt(50))));

			}

		}

		//第二个商品包邮，运费为0
		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.getOrderItems().get(1).setFreight(BigDecimal.ZERO);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	/**
	 * 
	 * 组合商品
	 * @throws InterruptedException 
	 * 
	 */
	private static PlaceOrderResp placeOrder9() throws InterruptedException {

		PlaceOrderReq placeorderBean = new PlaceOrderReq();

		SubcatalogsPo subcatalogsPo = new YmtProductsIWapper().getSubcatalogs();

		SubcatalogsPo subcatalogsPo2 = new YmtProductsIWapper()
				.getSubcatalogs();

		//组合商品
		String catalogId = subcatalogsPo.getCatalogId();

		placeorderBean = PlaceOrderService.getDefaultOrderReq();

		OrderItemVo OrderItemVo = placeorderBean.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems().get(0);

		OrderItemVo.setCatalogId(catalogId);

		OrderItemVo.setSubProduct(true);

		OrderItemVo.setFreight(new BigDecimal(new Random().nextInt(100)));

		OrderItemVo OrderItemVo2 = PlaceOrderService.getDefaultOrderItemVo();

		OrderItemVo2.setCatalogId(subcatalogsPo2.getCatalogId());

		OrderItemVo2.setFreight(new BigDecimal(new Random().nextInt(100)));

		OrderItemVo2.setSubProduct(true);

		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.getOrderItems().add(OrderItemVo2);

		placeorderBean.getOrderGroups().get(0).setSellerCoupon(null);

		placeorderBean.setYmatouCoupon(null);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_001() {
		Logger.start(true, "修改订单折扣和运费成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			// 保留两位小数
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			Logger.comment("前置数据准备结束");
			// 参数
			BigDecimal discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG,
			// changeorderdiscountcall.getMsg(), "验证Msg");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			HashMap m = new HashMap();
			m.put("OrderId", orderId);
			// m.put("UserId", UserId);
			Logger.verifyEquals("1.00",
					database.selectOrder(m).get(0).get("fOrderDiscount")
							.toString(), "验证fOrderDiscount");
			Logger.verifyEquals("5.00",
					database.selectOrder(m).get(0).get("fFreight").toString(),
					"验证fFreight");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_002() {
		Logger.start(true, "订单号不存在，修改订单折扣和运费失败");
		try {
			// 参数
			Integer orderId = 1334;
			int sellerId = 288484;
			BigDecimal discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_003() {
		Logger.start(true, "当前订单状态不能修改价格");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			// 保留两位小数
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			Logger.comment("前置数据准备结束");
			// 参数
			BigDecimal discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals(true, changeorderdiscountcall.getMsg()
					.contains("当前订单状态不能修改价格"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_004() {
		Logger.start(true, "当前订单使用了优惠券不可修改价格");
		try {
			// 创建订单

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(Discount);
			String DiscountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(DiscountTime);
			BigDecimal Freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(Freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_46,
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_005() {
		Logger.start(false, "订单号为null");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(Discount);
			String DiscountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(DiscountTime);
			BigDecimal Freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(Freight);
			// changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(111);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyIsContains("订单号不能为空",
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_006() {
		Logger.start(false, "操作用户Id为null");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(Discount);
			String DiscountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(DiscountTime);
			BigDecimal Freight = new BigDecimal(5);
			changeorderdiscountbean.setFreight(Freight);
			changeorderdiscountbean.setOrderId(1111);
			// changeorderdiscountbean.setSellerId(111);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("sellerId:买手ID未提供",
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_007() {
		Logger.start(false, "运费小于0");
		try {
			// 参数
			BigDecimal Discount = new BigDecimal(1);
			changeorderdiscountbean.setDiscount(Discount);
			String DiscountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(DiscountTime);
			BigDecimal Freight = new BigDecimal(-1);
			changeorderdiscountbean.setFreight(Freight);
			changeorderdiscountbean.setOrderId(1111);
			changeorderdiscountbean.setSellerId(111);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					changeorderdiscountcall.getCode(), "验证Code");
			Logger.verifyEquals("freight:" + ECode.FAILED201_MSG_71,
					changeorderdiscountcall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_008() {
		Logger.start(true, "一个订单下面有多个商品,修改订单折扣和运费成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = placeOrder6();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			// 参数
			BigDecimal discount = new BigDecimal(13);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(38);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// Logger.verifyEquals(ECode.FAILED201_MSG_46,changeorderdiscountcall.getMsg(),
			// "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_009() {
		Logger.start(true, "一个订单下面有多个商品,修改的订单折扣为负数");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = placeOrder6();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 参数
			BigDecimal discount = new BigDecimal(-10);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(38);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_010() {
		Logger.start(true, "一个订单下面有多个商品,修改折扣为涨价，校验付款后调整金额的分摊");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = placeOrder6();

			int userId = placeOrderResp.getUserId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			int tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(17);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(38);

			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();

			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证 
			verifyDb(orderId, BigDecimal.ZERO);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_011() {
		Logger.start(true, "一个订单下面有多个商品,修改折扣为降价,校验付款后调整金额的分摊");
		try {

			PlaceOrderResp placeOrderResp = placeOrder6();

			int userId = placeOrderResp.getUserId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			int tradingId = placeOrderResp.getTradingId();

			// 参数
			BigDecimal discount = new BigDecimal(-10);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(38);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			OrderCallService orderCallService = new OrderCallService();

			// 全款金额
			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额
			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证
			verifyDb(orderId, BigDecimal.ZERO);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_012() {
		Logger.start(true, "订单下多个商品，运费调整前是包邮，校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = placeOrder6();

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 参数
			BigDecimal discount = new BigDecimal(-10);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(138);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);

			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();

			OrderCallService orderCallService = new OrderCallService();

			// 该交易定金金额
			// 定金金额
			BigDecimal totalEarnestAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额
			// 付款
			orderCallService.orderPayRequest(userId, totalEarnestAmount,
					tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(orderId, BigDecimal.ZERO);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_013() {
		Logger.start(true, "订单下多个商品原来有运费,现增加运费,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderInfoPo> orderInfoList = order.getOrderInfoList();

			BigDecimal totalOrderInfoFreight = orderInfoList.stream()
					.map(item -> item.getFreight())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			BigDecimal orderInfoFreight1 = orderInfoList.get(0).getFreight();
			BigDecimal orderInfoFreight2 = orderInfoList.get(1).getFreight();
			BigDecimal orderInfoFreight3 = orderInfoList.get(2).getFreight();

			BigDecimal oldRate = orderInfoFreight1.divide(
					totalOrderInfoFreight, 2, BigDecimal.ROUND_DOWN);
			// 参数
			BigDecimal discount = new BigDecimal(-20);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = new BigDecimal(38);
			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			OrderCallService orderCallService = new OrderCallService();

			// 定金金额
			BigDecimal totalEarnestAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			// 付款
			orderCallService.orderPayRequest(userId, totalEarnestAmount,
					tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");

			// 数据库验证
			verifyDb(orderId, oldRate);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_014() {
		Logger.start(true, "订单下多个商品原有运费,现减少运费,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderInfoPo> orderInfoList = order.getOrderInfoList();

			BigDecimal totalOrderInfoFreight = orderInfoList.stream()
					.map(item -> item.getFreight())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			BigDecimal orderInfoFreight1 = orderInfoList.get(0).getFreight();
			BigDecimal orderInfoFreight2 = orderInfoList.get(1).getFreight();
			BigDecimal orderInfoFreight3 = orderInfoList.get(2).getFreight();

			BigDecimal oldRate = orderInfoFreight1.divide(
					totalOrderInfoFreight, 2, BigDecimal.ROUND_DOWN);
			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = totalOrderInfoFreight.subtract(new BigDecimal(
					2));

			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			OrderCallService orderCallService = new OrderCallService();

			// 定金金额
			BigDecimal totalEarnestAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			// 付款
			orderCallService.orderPayRequest(userId, totalEarnestAmount,
					tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证
			verifyDb(orderId, oldRate);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_015() {
		Logger.start(true, "订单下多个商品原有运费,调整运费为0,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder7();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderInfoPo> orderInfoList = order.getOrderInfoList();

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);
			BigDecimal freight = BigDecimal.ZERO;

			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			OrderCallService orderCallService = new OrderCallService();

			// 定金金额
			BigDecimal totalEarnestAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			// 付款
			orderCallService.orderPayRequest(userId, totalEarnestAmount,
					tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证
			verifyDb(orderId, BigDecimal.ZERO);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_016() {
		Logger.start(true, "订单下多个商品原有运费其中有商品运费为0,调整运费,校验运费分摊到商品逻辑");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder8();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderInfoPo> orderInfoList = order.getOrderInfoList();

			BigDecimal totalOrderInfoFreight = orderInfoList.stream()
					.map(item -> item.getFreight())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			BigDecimal orderInfoFreight1 = orderInfoList.get(0).getFreight();
			BigDecimal orderInfoFreight2 = orderInfoList.get(1).getFreight();
			BigDecimal orderInfoFreight3 = orderInfoList.get(2).getFreight();

			BigDecimal oldRate = orderInfoFreight1.divide(
					totalOrderInfoFreight, 2, BigDecimal.ROUND_DOWN);

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);

			BigDecimal freight = new BigDecimal(60);

			changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();
			OrderCallService orderCallService = new OrderCallService();

			// 定金金额
			BigDecimal totalEarnestAmount = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);// 支付金额

			// 付款
			orderCallService.orderPayRequest(userId, totalEarnestAmount,
					tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证
			verifyDb(orderId, oldRate);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_017() {
		Logger.start(true, "订单不包邮商品，调整运费为null,");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder8();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			//第一个订单下的商品列表
			List<OrderInfoPo> orderInfoList = order.getOrderInfoList();

			BigDecimal totalOrderInfoFreight = orderInfoList.stream()
					.map(item -> item.getFreight())
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			BigDecimal orderInfoFreight1 = orderInfoList.get(0).getFreight();
			BigDecimal orderInfoFreight2 = orderInfoList.get(1).getFreight();
			BigDecimal orderInfoFreight3 = orderInfoList.get(2).getFreight();

			BigDecimal oldRate = orderInfoFreight1.divide(
					totalOrderInfoFreight, 2, BigDecimal.ROUND_DOWN);

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);

			//changeorderdiscountbean.setFreight(freight);
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证

			// 订单主表
			List<Map> ymtOrders = new DefaultWapper()
					.selectOrderByOderId(orderId);

			DataUtil.verifyDecimalEquals(totalOrderInfoFreight,
					(BigDecimal) ymtOrders.get(0).get("fFreight"), "验证fFreight");

			DataUtil.verifyDecimalEquals(changeorderdiscountbean.getDiscount(),
					(BigDecimal) ymtOrders.get(0).get("fOrderDiscount"),
					"验证fOrderDiscount");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderDiscount_019() {
		Logger.start(true, "订单组合商品，调整运费,折扣");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = placeOrder9();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			int orderId = order.getOrderId();

			// 参数
			BigDecimal discount = new BigDecimal(28);
			changeorderdiscountbean.setDiscount(discount);
			String discountTime = YMTDateUtil.getSysTime();
			changeorderdiscountbean.setDiscountTime(discountTime);

			changeorderdiscountbean.setFreight(new BigDecimal(30));
			changeorderdiscountbean.setOrderId(orderId);
			changeorderdiscountbean.setSellerId(sellerId);
			// 发送请求
			changeorderdiscountcall.setData(changeorderdiscountbean);
			changeorderdiscountcall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changeorderdiscountcall.getCode(), "验证Code");
			// 数据库验证

			// 订单主表
			List<Map> ymtOrders = new DefaultWapper()
					.selectOrderByOderId(orderId);

			DataUtil.verifyDecimalEquals(changeorderdiscountbean.getDiscount(),
					(BigDecimal) ymtOrders.get(0).get("fOrderDiscount"),
					"验证fOrderDiscount");

			verifyPackageInfo(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDb(Integer orderId, BigDecimal oldRate) {

		// 订单主表
		List<Map> ymtOrders = database.selectOrderByOderId(orderId);

		// 订单商品明细
		List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);

		BigDecimal totalOrderInfoFreight = ymtOrdersInfoList.stream()
				.map(item -> (BigDecimal) item.get("fFreight"))
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		// 调整金额
		BigDecimal fdiscount = (BigDecimal) ymtOrders.get(0).get("fDiscount");

		// 总订单成交金额
		BigDecimal totalPrice = (BigDecimal) ymtOrders.get(0)
				.get("fTotalPrice");
		// 运费
		BigDecimal freight = (BigDecimal) ymtOrders.get(0).get("fFreight");

		// 申请调整的价格
		BigDecimal orderDiscount = changeorderdiscountbean.getDiscount()
				.setScale(2);
		// 申请调整的运费
		BigDecimal orderFreight = changeorderdiscountbean.getFreight()
				.setScale(2);

		Logger.verifyEquals(orderDiscount,
				(BigDecimal) ymtOrders.get(0).get("fOrderDiscount"),
				"验证fOrderDiscount");
		BigDecimal newPayableAmount = totalPrice.add(freight)
				.add(orderDiscount).add(fdiscount);
		Logger.verifyEquals(freight, orderFreight, "验证fFreight");
		// 应付金额调整
		Logger.verifyEquals(newPayableAmount, (BigDecimal) ymtOrders.get(0)
				.get("fPayableAmount"), "验证fPayableAmount");

		BigDecimal tempDicount = BigDecimal.ZERO;
		BigDecimal tempFreight = BigDecimal.ZERO;
		// 总调整金额
		BigDecimal discount = orderDiscount.add(fdiscount);

		Logger.verifyEquals(totalOrderInfoFreight, (BigDecimal) ymtOrders
				.get(0).get("fFreight"), "验证订单商品运费之和等于订单上运费");

		for (int i = 0; i < ymtOrdersInfoList.size(); i++) {

			// 订单商品信息表
			Map orderInfoMap = ymtOrdersInfoList.get(i);
			BigDecimal productPrice = (BigDecimal) orderInfoMap
					.get("fProductPrice");
			int proNum = (Integer) orderInfoMap.get("iAmount");

			BigDecimal rate = productPrice.multiply(new BigDecimal(proNum))
					.divide(totalPrice, 5, BigDecimal.ROUND_HALF_DOWN);

			BigDecimal productDiscount = BigDecimal.ZERO;

			BigDecimal productFreight = BigDecimal.ZERO;

			if (i == ymtOrdersInfoList.size() - 1) {
				productDiscount = discount.subtract(tempDicount);
				productFreight = freight.subtract(tempFreight);
			} else {
				// 商品里的调整分摊金额
				productDiscount = discount.multiply(rate).divide(
						BigDecimal.ONE, 2, BigDecimal.ROUND_HALF_UP);
				productFreight = freight.multiply(rate).divide(BigDecimal.ONE,
						2, BigDecimal.ROUND_HALF_UP);
			}

			tempDicount = tempDicount.add(productDiscount);
			tempFreight = tempFreight.add(productFreight);

			Logger.verifyEquals(productDiscount,
					(BigDecimal) orderInfoMap.get("fDiscount"),
					"验证订单商品fDiscount");

			//原来运费为空校验值
			if (!DataUtil.isBiggerThan(oldRate, BigDecimal.valueOf(0.000001))) {

				Logger.verifyEquals(productFreight,
						(BigDecimal) orderInfoMap.get("fFreight"),
						"验证订单商品fFreight");
				//原来有运费校验运费比例
			} else {

				if (i == 0) {

					DataUtil.verifyDecimalEquals(oldRate,
							((BigDecimal) orderInfoMap.get("fFreight")).divide(
									totalOrderInfoFreight, 2,
									BigDecimal.ROUND_DOWN), "验证订单商品调整后的运费比例");
				}
			}

		}
	}

	/**
	 * 校验组合商品落地数据
	 */
	private void verifyPackageInfo(int orderId) {

		try {

			Thread.sleep(5000);

			// 订单商品明细
			List<Map> ymtOrdersInfoList = database
					.selectOrderInfoByOderId(orderId);

			List<Map> ymtOrderSub = ymtOrdersInfoList.stream()
					.filter(item -> (Boolean) item.get("bSubProduct"))
					.collect(Collectors.toList());

			if (!CollectionUtils.isEmpty(ymtOrderSub)) {

				for (Map m : ymtOrderSub) {

					String sCatalogId = (String) m.get("sCatalogId");

					BigDecimal fProductPrice = (BigDecimal) m
							.get("fProductPrice");

					int iAmount = (Integer) m.get("iAmount");

					BigDecimal fFreight = (BigDecimal) m.get("fFreight");

					BigDecimal fDiscount = (BigDecimal) m.get("fDiscount");

					Logger.comment(String.format("校验sCatalogId：%s 的组合商品",
							sCatalogId));

					List<OrderInfoSubPo> OrderInfoSubPoList = database
							.getOrderInfoSub(orderId, sCatalogId);

					BigDecimal[] weight = new BigDecimal[OrderInfoSubPoList
							.size()];

					for (int j = 0; j < OrderInfoSubPoList.size(); j++) {

						OrderInfoSubPo orderInfoSubPo = OrderInfoSubPoList
								.get(j);

						weight[j] = orderInfoSubPo.getOriginalPrice().multiply(
								new BigDecimal(orderInfoSubPo.getNum()));
					}

					BigDecimal price[] = Ts_PlaceOrder.apportionByWeights2(
							fProductPrice.multiply(new BigDecimal(iAmount)),
							weight);

					BigDecimal freight[] = Ts_PlaceOrder.apportionByWeights2(
							fFreight, weight);

					BigDecimal discount[] = Ts_PlaceOrder.apportionByWeights2(
							fDiscount, weight);

					int k = 0;

					for (OrderInfoSubPo orderInfoSubPo : OrderInfoSubPoList) {

						Logger.comment("校验 Ymt_OrderInfoSub 表 ");

						Logger.comment("校验 SubOrderInfoId :"
								+ orderInfoSubPo.getSubOrderInfoId());

						DataUtil.verifyDecimalEquals(price[k],
								orderInfoSubPo.getPrice(), "校验Price");

						DataUtil.verifyDecimalEquals(freight[k],
								orderInfoSubPo.getFreight(), "校验Freight");
						DataUtil.verifyDecimalEquals(BigDecimal.ZERO,
								orderInfoSubPo.getYmtCoupon(), "校验YmtCoupon");
						DataUtil.verifyDecimalEquals(BigDecimal.ZERO,
								orderInfoSubPo.getSellerCoupon(),
								"校验SellerCoupon");
						DataUtil.verifyDecimalEquals(BigDecimal.ZERO,
								orderInfoSubPo.getPromotion(), "校验Promotion");

						DataUtil.verifyDecimalEquals(BigDecimal.ZERO,
								orderInfoSubPo.getThirdPartyDiscount(),
								"校验ThirdPartyDiscount");
						DataUtil.verifyDecimalEquals(discount[k],
								orderInfoSubPo.getDiscount(), "校验Discount");

						k++;

					}

				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
